package com.ms.education.course.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ms.education.JwtUtils;
import com.ms.education.R;
import com.ms.education.course.entity.ClassTeacher;
import com.ms.education.course.entity.ClassUser;
import com.ms.education.course.entity.Classroom;
import com.ms.education.course.entity.vo.ClassTeacherVo;
import com.ms.education.course.entity.vo.PageClassVo;
import com.ms.education.course.service.ClassTeacherService;
import com.ms.education.course.service.ClassUserService;
import com.ms.education.course.service.ClassroomService;
import com.ms.education.exceptionHandler.EduException;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 班级 前端控制器
 * </p>
 *
 * @author ms
 * @since 2023-02-24
 */
@RestController
@RequestMapping("/course/class")
public class ClassController {

    @Autowired
    private ClassroomService classroomService;

    @Autowired
    private ClassTeacherService classTeacherService;

    @Autowired
    private ClassUserService classUserService;

    @PostMapping("pageClass/{current}/{limit}")
    public R pageClass(@PathVariable("current") long current,
                       @PathVariable("limit") long limit,
                       @RequestBody PageClassVo pageClassVo) {

        Page<Classroom> page = classroomService.pageClass(current, limit, pageClassVo);
        long rows = page.getTotal();
        List<Classroom> classrooms = page.getRecords();
        return R.ok().data("rows", rows).data("classrooms", classrooms);
    }

    @GetMapping("getClassById/{classId}")
    public R getClassById(@PathVariable("classId") Integer classId) {
        Classroom classroom = classroomService.getById(classId);
        return R.ok().data("classroom", classroom);
    }

    @ApiOperation("添加班级")
    @PutMapping("addClass")
    public R addClass(Classroom classroom) {
        classroomService.saveClass(classroom);
        return R.ok();
    }

    @ApiOperation("修改班级信息")
    @PostMapping("updateClass")
    public R updateClass(@RequestBody Classroom classroom) {
        classroomService.updateById(classroom);
        return R.ok();
    }

    @ApiOperation("删除班级")
    @DeleteMapping("deleteClass/{id}")
    public R deleteClass(@PathVariable("id") Integer id) {
        classroomService.deleteClass(id);
        return R.ok();
    }


    @ApiOperation("为班级添加讲师")
    @PutMapping("addClassTeacher")
    public R addClassTeacher(ClassTeacher classTeacher) {
        classTeacherService.save(classTeacher);
        return R.ok();
    }


    @ApiOperation("根据id移除班级讲师")
    @DeleteMapping("deleteClassTeacher/{id}")
    public R deleteClassTeacher(@PathVariable("id") Integer id) {
        classTeacherService.removeById(id);
        return R.ok();
    }

    @ApiOperation("修改班级讲师权限")
    @PostMapping("updateClassTeacher")
    public R updateClassTeacher(@RequestBody ClassTeacher classTeacher) {
        classTeacherService.updateById(classTeacher);
        return R.ok();
    }

    @ApiOperation("查询班级讲师")
    @GetMapping("getClassTeacher/{classId}")
    public R getClassTeacher(@PathVariable("classId") Integer classId) {
        List<ClassTeacherVo> teachers = classroomService.getClassTeacher(classId);
        return R.ok().data("teachers", teachers);
    }

    @ApiOperation("查询班级学员id,用于远程调用")
    @GetMapping("getClassUserIds/{classId}")
    public List<Integer> getClassUserIds(@PathVariable("classId") Integer classId) {
        return classUserService.getClassUserIds(classId);
    }

    @ApiOperation("移除班级学员")
    @PostMapping("deleteClassUser")
    public R deleteClassUser(@RequestBody List<ClassUser> classUsers) {
        classUserService.deleteClassUser(classUsers);
        return R.ok();
    }

    @ApiOperation("添加班级学员")
    @PostMapping("addClassUser")
    public R addClassUser(@RequestBody List<ClassUser> classUsers) {
        classUserService.saveBatch(classUsers);
        return R.ok();
    }


}
